﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Verification.Cache;
using Verification.ChartUtils;
using Verification.JigsawPuzzle;
using Verification.Mold;
using Verification.Web.Models;

namespace Verification.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index(int x = 2, int y = 2, float W = 300, float H=250)
        {
            try
            {
                string rootPath = Directory.GetCurrentDirectory();
                string[] idlist = new string[x * y];
                JigsawPuzzle<string> jp = new JigsawPuzzle<string>();
                jp.X = 2;
                jp.Y = 2;

                for (int i = 0; i < idlist.Length; i++)
                {
                    idlist[i] = Guid.NewGuid().ToString();
                }
                jp.Path = rootPath + "\\wwwroot\\ZoomMap\\";
                jp.Data = idlist;
                jp.HttpContext = HttpContext;
                jp.FileType = "*.jpg";
                string[] idList = new string[x * y];
                string[] imgList = new string[x * y];
                jp.GetJigsawPuzzzleData(ref idList, ref imgList);
                ViewBag.Ids = idList;
                ViewBag.ImgList = imgList;
                ViewBag.rowNum = x;
                ViewBag.W = 100 / x;
                ViewBag.H = 100 / y;
            }
            catch(Exception ex)
            {

            }
            return View();
        }
        public object CheckVerifcation(string[] arr)
        {
            try
            {
                JigsawPuzzle<string> jp = new JigsawPuzzle<string>();
                jp.HttpContext = HttpContext;
                jp.CheckJigsawPuzzzle(arr);
                
                return "ok";
            }
            catch (Exception ex)
            {
                if(!string.IsNullOrWhiteSpace(((System.ArgumentException)ex).ParamName))
                    return ((System.ArgumentException)ex).ParamName;
                return ex.Message;
            }
        }

       
        public IActionResult Privacy()
        {
            Guid[] aaa = (Guid[])new CacheUtils().Get("VerCode_1");
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }

  
}
